/*
 * @file main.cpp
 * @author zhangpeihao
 */

#include <gtest/gtest.h>
#include <zsystem/core/common/types.h>
#include <zsystem/core/common/log.h>
#include <zsystem/core/common/assert.h>
#include <zsystem/core/common/alloc.h>
#include <zsystem/core/common/logdump.h>
#include "addressbook.pb.h"
#include "utcase.h"
#include <iostream>

using namespace std;

TEST_F(utcase, addressbook)
{
  // Create an object
  tutorial::Person person;
  person.set_id(1);
  person.set_name("zhang peihao");
  person.set_email("zhangpeihao@gmail.com");
  tutorial::Person::PhoneNumber* phone_number = person.add_phone();
  phone_number->set_type(tutorial::Person::MOBILE);
  phone_number->set_number("1302012XXXX");
  phone_number = person.add_phone();
  phone_number->set_type(tutorial::Person::HOME);
  phone_number->set_number("021-5882XXXX");

  // Dump to string buffer
  string dumpStr;
  person.SerializeToString(&dumpStr);
  Z_DUMP("stream length: " << dumpStr.size());

  // Convert from string buffer
  tutorial::Person p2;
  p2.ParseFromString(dumpStr);

  EXPECT_EQ(1, p2.id());
  EXPECT_STREQ("zhang peihao", p2.name().c_str());
  EXPECT_STREQ("zhangpeihao@gmail.com", p2.email().c_str());
  EXPECT_EQ(2, p2.phone_size());
  if (p2.phone_size() >= 1)
  {
    const tutorial::Person::PhoneNumber& phone = person.phone(0);
    EXPECT_EQ(tutorial::Person::MOBILE, phone.type());
    EXPECT_STREQ("1302012XXXX", phone.number().c_str());
  }
  if (p2.phone_size() >= 2)
  {
    const tutorial::Person::PhoneNumber& phone = person.phone(1);
    EXPECT_EQ(tutorial::Person::HOME, phone.type());
    EXPECT_STREQ("021-5882XXXX", phone.number().c_str());
  }

}


int main(int argc, char **argv) 
{
  google::InitGoogleLogging(argv[0]);
#ifdef __WIN32__
    google::SetLogDestination(INFO, "..\\..\\..\\logs\\INFO_");
    google::SetLogDestination(WARNING, "..\\..\\..\\logs\\WARNING_");
    google::SetLogDestination(ERROR, "..\\..\\..\\logs\\ERROR_");
#endif
#ifdef __LINUX__
    google::SetLogDestination(INFO, "/tmp/INFO_");
    google::SetLogDestination(WARNING, "/tmp/WARNING_");
    google::SetLogDestination(ERROR, "/tmp/ERROR_");
#endif
  ::testing::InitGoogleTest(&argc, argv);


  int r = RUN_ALL_TESTS();
#ifdef __WIN32__
  getchar();
#endif

  google::ShutdownGoogleLogging();

  return r;
}
